<template>
	<view class="segmented-control" :class="styleType === 'text' ? 'segmented-control--text' : 'segmented-control--button'" :style="[{borderColor: styleType === 'text' ? '' : activeColor}]">
		<view v-for="(item, index) in values" :key="index" v-if="show(item)" :class="{
			'segmented-control__item--button': styleType !== 'text',
			'segmented-control__item--button--active': index === currentIndex && styleType === 'button',
			'segmented-control__item--button--first': index === 0 && styleType === 'button',
			'segmented-control__item--button--last': index === values.length - 1 && styleType === 'button',
		}" :style="[{
			backgroundColor: index === currentIndex && styleType === 'button' ? (activeColor || themetool.color.main) : styleType === 'button' ? inActiveColor : 'transparent',
			borderColor: index === currentIndex && styleType === 'text' || styleType === 'button' ? (activeColor || themetool.color.main) : inActiveColor
		}]" class="segmented-control__item" @click="_onClick(index)">
			<view>
				<text :style="[
					index === currentIndex ? {
						color: styleType === 'text' ? (activeColor || themetool.color.main) : '#fff'
					} : {
						color: styleType === 'text' ? '#000' : (activeColor || themetool.color.main)
					}, {fontSize: getFontSize}
				]" class="segmented-control__text" :class="styleType === 'text' && index === currentIndex ? 'segmented-control__item--text': ''">{{lang(itemShow(item))}}</text>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * SegmentedControl 分段器
	 * @description 用作不同视图的显示
	 * @tutorial https://ext.dcloud.net.cn/plugin?id=54
	 * @property {Number} current 当前选中的tab索引值，从0计数
	 * @property {Number, String} value 当前选中值
	 * @property {Number, String} modelValue 当前选中值
	 * @property {String} styleType = [button|text|scroll] 分段器样式类型
	 * 	@value button 按钮类型
	 * 	@value text 文字类型
	 * @property {String} activeColor 选中的标签背景色与边框颜色
	 * @property {String} inActiveColor 未选中的标签背景色与边框颜色
	 * @property {Array} values 选项数组。可以是纯文本，也可以是对象，默认是{text: '', value: '', disable: true}
	 * @event {Function} clickItem 组件触发点击事件时触发，e={currentIndex}
	 */

	import base from '@/components/index.vue'
	
	export default {
		extends: base,
		name: 'UniSegmentedControl',
		emits: ['clickItem', 'change'],
		props: {
			// modelValue、value、current默认值。优先级从高到低
			modelValue: {
				type: [Number, String],
				default: undefined
			},
			value: {
				type: [Number, String],
				default: undefined
			},
			current: {
				type: Number,
				default: -1
			},
			values: {
				type: Array,
				default () {
					return []
				}
			},
			activeColor: String,
			inActiveColor: {
				type: String,
				default: 'transparent'
			},
			styleType: {
				type: String,
				default: 'button'
			},
			fontSize: String,
			map: {
				type: Object,
				default () {
					return {
						text: 'text',
						value: 'value',
						disable: 'disable'
					}
				}
			}
		},
		watch: {
			current(value) {
				this.emit(value)
				this.currentIndex = value
			}
		},
		data() {
			return {
				currentIndex: 0,
			}
		},
		computed: {
			themetool() {
				return this.core.theme
			},
			getFontSize() {
				var fontSize = this.fontSize
				if (!fontSize) {
					fontSize = '14px'
				}
				return fontSize
			}
		},
		mounted() {
			this.initial()
		},
		methods: {
			initial() {
				let currentIndex = this.getCurrentIndex(this.modelValue)
				if (currentIndex === -1) {
					currentIndex = this.getCurrentIndex(this.value)
				}
				if (currentIndex === -1) {
					currentIndex = this.current
					this.emit(currentIndex)
				}
				this.currentIndex = currentIndex
			},
			
			// 通过value获取当前索引值
			getCurrentIndex(value) {
				if (value || value === '' || value === 0) {
					for (let i = 0; i < this.values.length; i++) {
						const item = this.values[i]
						if (typeof(item) === 'string') {
							if (item === value) {
								return i
							}
						} else {
							if (item[this.map.value || 'value'] === value) {
								return i
							}
						}
					}
				}
				return -1
			},
			
			show(item) {
				if (typeof(item) === 'string') {
					return true
				}
				return !item[this.map.disable || 'disable']
			},
			
			itemShow(item) {
				if (typeof(item) === 'string') {
					return item
				}
				return item[this.map.text || 'text'] || ''
			},
			
			_onClick(index, forAll) {
				if (this.currentIndex !== index) {
					this.currentIndex = index
					this.$emit('clickItem', {
						currentIndex: index
					})
					this.emit(index)
				}
			},
			
			emit(index) {
				if (index > -1 && index < this.values.length) {
					let value = this.values[index]
					if (typeof(value) === 'string') {
						
					} else {
						if (value) {
							value = value[this.map.value || 'value']
						}
					}
					this.$emit('input', value)
					this.$emit('update:modelValue', value)
					this.$emit('change', value)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.segmented-control {
		/* #ifndef APP-NVUE */
		display: flex;
		box-sizing: border-box;
		/* #endif */
		flex-direction: row;
		height: 36px;
		overflow: hidden;
		/* #ifdef H5 */
		cursor: pointer;
		/* #endif */
	}

	.segmented-control__item {
		/* #ifndef APP-NVUE */
		display: inline-flex;
		box-sizing: border-box;
		/* #endif */
		position: relative;
		flex: 1;
		justify-content: center;
		align-items: center;
	}

	.segmented-control__item--button {
		border-style: solid;
		border-top-width: 1px;
		border-bottom-width: 1px;
		border-right-width: 1px;
		border-left-width: 0;
	}

	.segmented-control__item--button--first {
		border-left-width: 1px;
		border-top-left-radius: 5px;
		border-bottom-left-radius: 5px;
	}

	.segmented-control__item--button--last {
		border-top-right-radius: 5px;
		border-bottom-right-radius: 5px;
	}

	.segmented-control__item--text {
		border-bottom-style: solid;
		border-bottom-width: 2px;
		padding: 6px 0;
	}

	.segmented-control__text {
		font-size: 14px;
		line-height: 20px;
		text-align: center;
	}
	
	.scroll-view {
		width: 100%;
		white-space: nowrap;
		scrollbar-width: none; /* 对于微信小程序 */
		
		::-webkit-scrollbar { /* 对于web */
		  display: none; /* 隐藏滚动条 */
		  // width: 0 !important; /* 可选，滚动条宽度为0 */
		  // height: 0 !important; /* 滚动条高度为0 */
		  // -webkit-appearance: none; /* 移除默认滚动条样式 */
		  // background: transparent; /* 背景透明 */
		}
	}
</style>
